package cn.iocoder.yudao.module.famous.doctor.service.banner;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.banner.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.banner.FamBannerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.banner.FamBannerConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.banner.FamBannerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 名医-轮播图 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FamBannerServiceImpl implements FamBannerService {

    @Resource
    private FamBannerMapper famBannerMapper;

    @Override
    public Long createFamBanner(FamBannerCreateReqVO createReqVO) {
        // 插入
        FamBannerDO famBanner = FamBannerConvert.INSTANCE.convert(createReqVO);
        if (famBanner.getContent() == null){
            famBanner.setContent("");
        }
        famBannerMapper.insert(famBanner);
        // 返回
        return famBanner.getId();
    }

    @Override
    public void updateFamBanner(FamBannerUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamBannerExists(updateReqVO.getId());
        // 更新
        FamBannerDO updateObj = FamBannerConvert.INSTANCE.convert(updateReqVO);
        famBannerMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamBanner(Long id) {
        // 校验存在
        validateFamBannerExists(id);
        // 删除
        famBannerMapper.deleteById(id);
    }

    private void validateFamBannerExists(Long id) {
        if (famBannerMapper.selectById(id) == null) {
            throw exception(FAM_BANNER_NOT_EXISTS);
        }
    }

    @Override
    public FamBannerDO getFamBanner(Long id) {
        return famBannerMapper.selectById(id);
    }

    @Override
    public List<FamBannerDO> getFamBannerList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famBannerMapper.selectBatchIds(ids);
    }

    @Override
    public List<FamBannerDO> getFamBannerList(Integer deviceType) {
        if (deviceType == null){
            deviceType = 1;
        }
        return famBannerMapper.selectList(new LambdaQueryWrapperX<FamBannerDO>()
                .eq(FamBannerDO::getStatus,1)
                .eq(FamBannerDO::getType,deviceType));
    }

    @Override
    public PageResult<FamBannerDO> getFamBannerPage(FamBannerPageReqVO pageReqVO) {
        return famBannerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamBannerDO> getFamBannerList(FamBannerExportReqVO exportReqVO) {
        return famBannerMapper.selectList(exportReqVO);
    }

}
